using System;
using System.Collections.Generic;
using System.Text;

using DslModeling = global::Microsoft.VisualStudio.Modeling;
using DslDiagrams = global::Microsoft.VisualStudio.Modeling.Diagrams;

namespace Evaluant.EussDSL
{
    /// <summary>
    /// Custom "Mapping" tag which MUST map with a valid mapping file declaration
    /// </summary>
    public partial class EussMappingSerializer : DslModeling::DomainClassXmlSerializer
    {
        private void CustomConstructor()
        {
            DefaultConstructor();
        }

        private string CustomXmlTagName
        {
            get { return DefaultXmlTagName; }
        }

        private string CustomMonikerTagName
        {
            get { return DefaultMonikerTagName; }
        }

        private string CustomMonikerAttributeName
        {
            get { return DefaultMonikerAttributeName; }
        }

        private void CustomRead(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader)
        {
            DefaultRead(serializationContext, element, reader);
        }

        private static void CustomReadPropertiesFromAttributes(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader)
        {
            DefaultReadPropertiesFromAttributes(serializationContext, element, reader);
        }

        private static void CustomReadElements(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlReader reader)
        {
            DefaultReadElements(serializationContext, element, reader);
        }

        private DslModeling::ModelElement CustomTryCreateInstance(DslModeling::SerializationContext serializationContext, global::System.Xml.XmlReader reader, DslModeling::Partition partition)
        {
            return DefaultTryCreateInstance(serializationContext, reader, partition);
        }

        private DslModeling::ModelElement CustomCreateInstance(DslModeling::SerializationContext serializationContext, global::System.Xml.XmlReader reader, DslModeling::Partition partition)
        {
            return DefaultCreateInstance(serializationContext, reader, partition);
        }

        private DslModeling::Moniker CustomTryCreateMonikerInstance(DslModeling::SerializationContext serializationContext, global::System.Xml.XmlReader reader, DslModeling::ModelElement sourceRolePlayer, global::System.Guid relDomainClassId, DslModeling::Partition partition)
        {
            return DefaultTryCreateMonikerInstance(serializationContext, reader, sourceRolePlayer, relDomainClassId, partition);
        }

        private DslModeling::Moniker CustomCreateMonikerInstance(DslModeling::SerializationContext serializationContext, global::System.Xml.XmlReader reader, DslModeling::ModelElement sourceRolePlayer, global::System.Guid relDomainClassId, DslModeling::Partition partition)
        {
            return DefaultCreateMonikerInstance(serializationContext, reader, sourceRolePlayer, relDomainClassId, partition);
        }

        private void CustomWriteMoniker(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer, DslModeling::ModelElement sourceRolePlayer, DslModeling::DomainRelationshipXmlSerializer relSerializer)
        {
            DefaultWriteMoniker(serializationContext, element, writer, sourceRolePlayer, relSerializer);
        }

        private void CustomWrite(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer, DslModeling::RootElementSettings rootElementSettings)
        {
            //DefaultWrite(serializationContext, element, writer, rootElementSettings);

            #region Check Parameters
            global::System.Diagnostics.Debug.Assert(serializationContext != null);
            if (serializationContext == null)
                throw new global::System.ArgumentNullException("serializationContext");
            global::System.Diagnostics.Debug.Assert(element != null);
            if (element == null)
                throw new global::System.ArgumentNullException("element");
            global::System.Diagnostics.Debug.Assert(writer != null);
            if (writer == null)
                throw new global::System.ArgumentNullException("writer");
            #endregion

            // Write start of element, including schema target namespace if specified.
            if (rootElementSettings != null && !string.IsNullOrEmpty(rootElementSettings.SchemaTargetNamespace))
            {
                writer.WriteStartElement(this.XmlTagName);
                //writer.WriteStartAttribute("xsi", "xsi", "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xmlns", "xsd", null, "http://www.w3.org/2001/XMLSchema"); 
                //writer.WriteStartElement(this.XmlTagName, rootElementSettings.SchemaTargetNamespace);
            }
            else
            {
                writer.WriteStartElement(this.XmlTagName);
            }

            // Write version info (in the format 1.2.3.4), if necessary
            //if (rootElementSettings != null && rootElementSettings.Version != null)
            //    writer.WriteAttributeString("dslVersion", rootElementSettings.Version.ToString(4));

            // Write out element Id.
            writer.WriteAttributeString("Id", element.Id.ToString("D", global::System.Globalization.CultureInfo.CurrentCulture));

            CustomWritePropertiesAsAttributes(serializationContext, element, writer);

            if (!serializationContext.Result.Failed)
            {
                // Write 1) properties serialized as nested XML elements and 2) child model elements into XML.
                DefaultWriteElements(serializationContext, element, writer);
            }

            writer.WriteEndElement();
        }

        private static void CustomWritePropertiesAsAttributes(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer)
        {
            //DefaultWritePropertiesAsAttributes(serializationContext, element, writer);

            //EussMapping instanceOfEussMapping = element as EussMapping;
            //global::System.Diagnostics.Debug.Assert(instanceOfEussMapping != null, "Expecting an instance of EussMapping");

            //// xmlnsxsi
            //if (!serializationContext.Result.Failed)
            //{
            //    global::System.String propValue = instanceOfEussMapping.xsi;
            //    if (!serializationContext.Result.Failed)
            //    {
            //        writer.WriteAttributeString("xmlns","xsi", "eee", propValue);
            //    }
            //}
            //// xmlnsxsd
            //if (!serializationContext.Result.Failed)
            //{
            //    global::System.String propValue = instanceOfEussMapping.xsd;
            //    if (!serializationContext.Result.Failed)
            //    {
            //        if (propValue != null && (serializationContext.WriteOptionalPropertiesWithDefaultValue || string.CompareOrdinal(propValue, "http://www.w3.org/2001/XMLSchema") != 0))
            //        {	// No need to write the value out if it's the same as default value.
            //            writer.WriteAttributeString("xmlns", "xsd", "eee", propValue);
            //        }
            //    }
            //}
        }

        private static void CustomWriteElements(DslModeling::SerializationContext serializationContext, DslModeling::ModelElement element, global::System.Xml.XmlWriter writer)
        {
            DefaultWriteElements(serializationContext, element, writer);
        }

        private string CustomCalculateQualifiedName(DslModeling::DomainXmlSerializerDirectory directory, DslModeling::ModelElement element)
        {
            return DefaultCalculateQualifiedName(directory, element);
        }

        private string CustomGetMonikerQualifier(DslModeling::DomainXmlSerializerDirectory directory, DslModeling::ModelElement element)
        {
            return DefaultGetMonikerQualifier(directory, element);
        }
    }
}
